package com.doctor.windflower_doctor.activity;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.RefreshIntent;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class PostReportActivity extends BaseActivity implements Constent {
	private RadioButton radioButton1;
	private RadioButton radioButton2;
	private RadioButton radioButton3;
	private RadioButton radioButton4;
	private RadioButton radioButton5;
	private String reportName;
	private Button report_Button;
	private EditText report_EditText;
	private String questionId;
	private SharedPreferencesDB db;
	private ImageButton back;
	

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		radioButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioButton2.setChecked(false);
					radioButton3.setChecked(false);
					radioButton4.setChecked(false);
					radioButton5.setChecked(false);
					report_EditText.setEnabled(false);
					reportName = buttonView.getText().toString();
					report_EditText.setText("");
				}
			}
		});
		radioButton2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioButton1.setChecked(false);
					radioButton3.setChecked(false);
					radioButton4.setChecked(false);
					radioButton5.setChecked(false);
					report_EditText.setEnabled(false);
					reportName = buttonView.getText().toString();
					report_EditText.setText("");
				}
			}
		});
		radioButton3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioButton1.setChecked(false);
					radioButton2.setChecked(false);
					radioButton4.setChecked(false);
					radioButton5.setChecked(false);
					report_EditText.setEnabled(false);
					reportName = buttonView.getText().toString();
					report_EditText.setText("");
				}
			}
		});
		radioButton4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioButton1.setChecked(false);
					radioButton2.setChecked(false);
					radioButton3.setChecked(false);
					radioButton5.setChecked(false);
					report_EditText.setEnabled(false);
					reportName = buttonView.getText().toString();
					report_EditText.setText("");
					
				}
			}
		});
		radioButton5.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioButton1.setChecked(false);
					radioButton2.setChecked(false);
					radioButton3.setChecked(false);
					radioButton4.setChecked(false);
					report_EditText.setEnabled(true);

				}
			}
		});
		report_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!radioButton1.isChecked()&&!radioButton2.isChecked()&&!radioButton3.isChecked()&&!radioButton4.isChecked()&&!radioButton5.isChecked()){
					Toast.makeText(PostReportActivity.this, "请选择举报内容", Toast.LENGTH_SHORT).show();
				}else{
					if(radioButton5.isChecked()&&!report_EditText.getText().toString().trim().equals("")&&report_EditText.getText().toString().trim().length()<=50){
						postTimeContent(questionId, report_EditText.getText().toString());
					}else if(radioButton5.isChecked()&&report_EditText.getText().toString().equals("")){
						Toast.makeText(PostReportActivity.this, "请输入0-50字之间的举报原因，谢谢合作!",
								Toast.LENGTH_SHORT).show();
					}else{
						postTime(questionId);
					}
				}
			
			

			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		db = SharedPreferencesDB.getInstance(PostReportActivity.this);
		report_Button = (Button) findViewById(R.id.report_Button);
		report_EditText = (EditText) findViewById(R.id.report_EditText);
		questionId = intent.getStringExtra("questionId");
		back = (ImageButton) findViewById(R.id.imageButton_back);
		radioButton1 = (RadioButton) findViewById(R.id.b1);
		radioButton2 = (RadioButton) findViewById(R.id.b2);
		radioButton3 = (RadioButton) findViewById(R.id.b3);
		radioButton4 = (RadioButton) findViewById(R.id.b4);
		radioButton5 = (RadioButton) findViewById(R.id.b5);
	
		report_EditText.setOnEditorActionListener(new OnEditorActionListener() {            
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
    });
		report_EditText.setEnabled(false);
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.postreportactivity;
	}

	private void postTime(String questionId) {

		HttpMethod method = HttpMethod.POST;
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(
				new JsonParser(), QuestionAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<QuestionAction> responseInfo) {
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						startActivity(new Intent(PostReportActivity.this,
								MainActivity.class));
						Toast.makeText(PostReportActivity.this, "您的举报提交成功，感谢您的帮助！",
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(PostReportActivity.this,
								responseInfo.success.message, 1).show();
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub
				Toast.makeText(PostReportActivity.this,
						"请检查网络状态", Toast.LENGTH_SHORT).show();

			}
		};
		HttpRequestParams params = new HttpRequestParams(method, URL+"ask/doctor/report",
				requestCallBack);
		params.addBodyParameter("content", reportName);
		params.addBodyParameter("questionId", questionId);
		params.addBodyParameter("doctorId", db.getDocotorId());
		HttpRequest.send(params);
	}

	private void postTimeContent(String questionId, String content) {

		HttpMethod method = HttpMethod.POST;
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(
				new JsonParser(), QuestionAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<QuestionAction> responseInfo) {
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						startActivity(new Intent(PostReportActivity.this,
								MainActivity.class));
						Toast.makeText(PostReportActivity.this, "您的举报提交成功，感谢您的帮助！",
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(PostReportActivity.this, "请检查网络连接",
								Toast.LENGTH_SHORT).show();
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub

			}
		};
		HttpRequestParams params = new HttpRequestParams(method, URL+"ask/doctor/report",
				requestCallBack);
		params.addBodyParameter("content", content);
		params.addBodyParameter("questionId", questionId);
		params.addBodyParameter("doctorId", db.getDocotorId());
		HttpRequest.send(params);
	}
}
